package cn.org.seventh.tools.plugin;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

public class MethodUrlViewPlugin extends QDoxPlugin {

	private String filename = "views.properties";

	private String basepath = "/WEB-INF/views";

	public MethodUrlViewPlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public MethodUrlViewPlugin(FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public String getName(Object metadata) throws Exception {
		String str = getNamedParameter(metadata, "name", null);
		if (str == null || "".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		return str;
	}

	public String getType(Object metadata) {
		return getNamedParameter(metadata, "type", null);
	}

	public String getPath(Object metadata) throws Exception {
		if (getType(metadata).equals("pdf")
				|| getType(metadata).equals("excel")) {
			return getNamedParameter(metadata, "path", null);
		} else {
			String str = getNamedParameter(metadata, "path", null);
			if (str == null || "".equals(str)) {
				throw new Exception("value must not be  empty! ");
			}
			if (getType(metadata).equals("tiles")) {
				return str;
			} else {
				if (!str.startsWith("/")) {
					return "/" + str;
				} else {
					return str;
				}
			}
		}
	}

	public String getClass(Object metadata) throws Exception {
		String str = getNamedParameter(metadata, "class", null);
		if (str == null || "".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		return str;
	}

	public String getUrl(Object metadata) throws Exception {
		String str = getNamedParameter(metadata, "url", null);
		if (str == null || "".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		if (!str.startsWith("/")) {
			str = "/" + str;
		}
		if (!str.endsWith("/")) {
			str = str + "/";
		}
		return str;
	}

	public String getMeta(Object metadata) throws Exception {
		String str = getNamedParameter(metadata, "meta", null);
		if (str != null && "".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		return str;
	}

	private String getNamedParameter(Object metadata, String name, String value) {
		JavaMethod javaMethod = (JavaMethod) metadata;
		String str = javaMethod.getNamedParameter(MethodUrlTagImpl.NAME, name);
		return null == str ? value : str.trim();
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(SpringWebTagImpl.NAME) == null ? false
				: true;
	}

	public String getDestinationFilename(Object metadata) {
		return filename;
	}

	public String getDestinationPackage(Object metadata) {
		return ".";
	}

	public String getFilename() {
		return this.filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getBasepath() {
		if (null != this.basepath && !"".equals(this.basepath.trim())) {
			if (!this.basepath.startsWith("/")) {
				this.basepath = "/" + this.basepath;
			}
			if (this.basepath.endsWith("/")) {
				this.basepath = this.basepath.substring(0, this.basepath
						.length() - 1);
			}
			return this.basepath;
		} else {
			return "/WEB-INF/views";
		}
	}

	public void setBasepath(String basepath) {
		this.basepath = basepath;
	}
}